﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Services
{
    /// <summary>
    /// MongoDb服务
    /// </summary>
    public class MongoDbServices
    {
        private static readonly string ConnectionString = ConfigurationManager.AppSettings["MongoDbConnection"];
        // private const string CONNECTION_STRING = "mongodb://TCSceneryZhuanTi:IYT7yzyGY3nareFpnkts2pc3acCUUg@172.16.6.54:10000,172.16.6.55:10000,172.16.6.56:10000/TCSceneryZhuanTi/?readPreference=SecondaryPreferred";
        private static MongoClient _client;
        private const string DbName = "Quetion";
        public static MongoClient Client
        {
            get
            {
                if (_client != null) return _client;
                _client = new MongoClient(ConnectionString);

                return _client;
            }
        }

        public static IMongoCollection<T> GetCollection<T>(string dbName, string collName)
        {
            var db = Client.GetDatabase(dbName);

            return db.GetCollection<T>(collName);
        }
        #region MongDb增改查泛型方法

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="document"></param>
        /// <param name="collName"></param>
        /// <returns></returns>
        public Task InsertMongoDb<T>(T document, string collName)
        {
            var coll = GetCollection<T>(DbName, collName);
            return coll.InsertOneAsync(document);
        }

        /// <summary>
        /// 根据条件查询一条数据
        /// </summary>
        /// <param name="collName">表名</param>
        /// <param name="filter">过滤条件</param>
        /// <returns></returns>
        public Task<T> SimpleQueryMongoDb<T>(string collName, FilterDefinition<T> filter)
        {
            var coll = GetCollection<T>(DbName, collName);
            var doc = coll.Find(filter).FirstOrDefaultAsync();

            return doc;
        }

        /// <summary>
        /// mongodb条件查询
        /// </summary>
        /// <param name="collName"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public Task<List<T>> QueryMongoDb<T>(string collName, FilterDefinition<T> filter)
        {
            var coll = GetCollection<T>(DbName, collName);
            return coll.Find(filter).ToListAsync();
        }

        /// <summary>
        /// 实体更新
        /// </summary>
        /// <param name="document">内容</param>
        /// <param name="collName">表名</param>
        /// <param name="filter">条件</param>
        /// <returns></returns>
        public Task<ReplaceOneResult> ReplaceMongoDb<T>(T document, string collName, FilterDefinition<T> filter)
        {
            var coll = GetCollection<T>(DbName, collName);
            return coll.ReplaceOneAsync(filter, document);
        }

        /// <summary>
        ///  更新域更新
        /// </summary>
        /// <param name="collName"></param>
        /// <param name="filter"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public Task<UpdateResult> UpdateMongoDb<T>(string collName, FilterDefinition<T> filter, UpdateDefinition<T> update)
        {
            var coll = GetCollection<T>(DbName, collName);
            return coll.UpdateOneAsync(filter, update);
        }

        #endregion
    }
}
